{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results_path='/media/taufiq/Data/heart_sound/Heart_Sound/codes/logs/results.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv(results_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_entry = {'Filename':'logname','Weight Initialization':'he_normal',\n",
    "             'Activation':'relu-sigmoid','Class weights':'addweights',\n",
    "             'Kernel Size':'kernel_size','Max Norm':'maxnorm',\n",
    "             'Dropout -filters':'dropout_rate',\n",
    "             'Dropout - dense':'dropout_rate_dense',\n",
    "             'L2 - filters':'l2_reg','L2- dense':'l2_reg_dense',\n",
    "             'Batch Size':'batch_size','Optimizer':'Adam','Learning Rate':'lr',\n",
    "             'BN momentum':'bn_momentum'}\n",
    "index,_=df.shape\n",
    "new_entry = pd.DataFrame(new_entry,index=[index])\n",
    "#trial = pd.concat([df,new_entry]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1 = pd.concat([df,new_entry],axis=0)\n",
    "df1.head()\n",
    "df1 = df1.reindex(df.columns,axis=1)\n",
    "df1.to_csv(results_path,index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_path='/media/taufiq/Data/heart_sound/Heart_Sound/codes/logs/training.csv'\n",
    "df2 = pd.read_csv(train_path)\n",
    "max_idx = df2['val_macc'].idxmax()\n",
    "df2.loc[[max_idx]]['val_macc'].values[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setting manually for testing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "epochs=200\n",
    "batch_size=64\n",
    "log_name='fold0aug 2018-01-07 19:49:01.116821'\n",
    "log_dir= '/media/taufiq/Data/heart_sound/Heart_Sound/codes/logs/'\n",
    "results_path='/media/taufiq/Data/heart_sound/Heart_Sound/codes/logs/results.csv'\n",
    "addweights=False\n",
    "bn_momentum = 0.99\n",
    "eps= 1.1e-5\n",
    "bias=False\n",
    "l2_reg=0.01\n",
    "l2_reg_dense=0.\n",
    "kernel_size=5\n",
    "maxnorm=10000.\n",
    "dropout_rate=0.5\n",
    "dropout_rate_dense=0.\n",
    "padding='valid'\n",
    "activation_function='relu'\n",
    "subsam=2\n",
    "\n",
    "lr=0.0007\n",
    "lr_decay=1e-8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Filename</th>\n",
       "      <th>Weight Initialization</th>\n",
       "      <th>Activation</th>\n",
       "      <th>Class weights</th>\n",
       "      <th>Kernel Size</th>\n",
       "      <th>Max Norm</th>\n",
       "      <th>Dropout -filters</th>\n",
       "      <th>Dropout - dense</th>\n",
       "      <th>L2 - filters</th>\n",
       "      <th>L2- dense</th>\n",
       "      <th>...</th>\n",
       "      <th>BN momentum</th>\n",
       "      <th>Best Val Acc Per Cardiac Cycle</th>\n",
       "      <th>Epoch</th>\n",
       "      <th>Training Acc per cardiac cycle</th>\n",
       "      <th>Sensitivity</th>\n",
       "      <th>Specificity</th>\n",
       "      <th>Macc</th>\n",
       "      <th>No of Abnormal Strongly Mislabeled</th>\n",
       "      <th>No of Normal Strongly Mislabeled</th>\n",
       "      <th>Comments</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>fold2 2018-01-02 15:17:15.166515</td>\n",
       "      <td>he_normal</td>\n",
       "      <td>relu-sigmoid</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.99</td>\n",
       "      <td>85.280000</td>\n",
       "      <td>69.0</td>\n",
       "      <td>89.75000</td>\n",
       "      <td>81.29000</td>\n",
       "      <td>94.740000</td>\n",
       "      <td>88.010000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>increasing dense regularization ruined it</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>fold0aug 2018-01-06 17:49:28.443969</td>\n",
       "      <td>he_normal</td>\n",
       "      <td>relu-sigmoid</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.99</td>\n",
       "      <td>74.730000</td>\n",
       "      <td>180.0</td>\n",
       "      <td>96.26000</td>\n",
       "      <td>86.75000</td>\n",
       "      <td>68.490000</td>\n",
       "      <td>77.620000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>fold0aug 2018-01-06 20:09:21.292214</td>\n",
       "      <td>he_normal</td>\n",
       "      <td>relu-sigmoid</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.005</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.99</td>\n",
       "      <td>73.720000</td>\n",
       "      <td>91.0</td>\n",
       "      <td>95.25000</td>\n",
       "      <td>82.12000</td>\n",
       "      <td>71.920000</td>\n",
       "      <td>77.020000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>fold0aug 2018-01-07 11:48:42.276690</td>\n",
       "      <td>he_normal</td>\n",
       "      <td>relu-sigmoid</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.25</td>\n",
       "      <td>0.007</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.99</td>\n",
       "      <td>72.860000</td>\n",
       "      <td>154.0</td>\n",
       "      <td>94.95000</td>\n",
       "      <td>70.86000</td>\n",
       "      <td>82.880000</td>\n",
       "      <td>76.870000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>fold0aug 2018-01-07 19:49:01.116821</td>\n",
       "      <td>he_normal</td>\n",
       "      <td>relu-sigmoid</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.010</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.99</td>\n",
       "      <td>73.580011</td>\n",
       "      <td>180.0</td>\n",
       "      <td>95.28438</td>\n",
       "      <td>84.10596</td>\n",
       "      <td>69.863014</td>\n",
       "      <td>76.984487</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                               Filename Weight Initialization    Activation  \\\n",
       "23     fold2 2018-01-02 15:17:15.166515             he_normal  relu-sigmoid   \n",
       "24  fold0aug 2018-01-06 17:49:28.443969             he_normal  relu-sigmoid   \n",
       "25  fold0aug 2018-01-06 20:09:21.292214             he_normal  relu-sigmoid   \n",
       "26  fold0aug 2018-01-07 11:48:42.276690             he_normal  relu-sigmoid   \n",
       "27  fold0aug 2018-01-07 19:49:01.116821             he_normal  relu-sigmoid   \n",
       "\n",
       "    Class weights  Kernel Size  Max Norm  Dropout -filters  Dropout - dense  \\\n",
       "23            NaN            5   10000.0               0.5             0.25   \n",
       "24            NaN            5   10000.0               0.5             0.25   \n",
       "25            NaN            5   10000.0               0.5             0.25   \n",
       "26            NaN            5   10000.0               0.5             0.25   \n",
       "27            0.0            5   10000.0               0.5             0.00   \n",
       "\n",
       "    L2 - filters  L2- dense                    ...                      \\\n",
       "23         0.000        0.0                    ...                       \n",
       "24         0.000        0.0                    ...                       \n",
       "25         0.005        0.0                    ...                       \n",
       "26         0.007        0.0                    ...                       \n",
       "27         0.010        0.0                    ...                       \n",
       "\n",
       "    BN momentum Best Val Acc Per Cardiac Cycle  Epoch  \\\n",
       "23         0.99                      85.280000   69.0   \n",
       "24         0.99                      74.730000  180.0   \n",
       "25         0.99                      73.720000   91.0   \n",
       "26         0.99                      72.860000  154.0   \n",
       "27         0.99                      73.580011  180.0   \n",
       "\n",
       "    Training Acc per cardiac cycle  Sensitivity   Specificity       Macc  \\\n",
       "23                        89.75000      81.29000    94.740000  88.010000   \n",
       "24                        96.26000      86.75000    68.490000  77.620000   \n",
       "25                        95.25000      82.12000    71.920000  77.020000   \n",
       "26                        94.95000      70.86000    82.880000  76.870000   \n",
       "27                        95.28438      84.10596    69.863014  76.984487   \n",
       "\n",
       "    No of Abnormal Strongly Mislabeled  No of Normal Strongly Mislabeled  \\\n",
       "23                                 NaN                               NaN   \n",
       "24                                 NaN                               NaN   \n",
       "25                                 NaN                               NaN   \n",
       "26                                 NaN                               NaN   \n",
       "27                                 NaN                               NaN   \n",
       "\n",
       "                                     Comments  \n",
       "23  increasing dense regularization ruined it  \n",
       "24                                        NaN  \n",
       "25                                        NaN  \n",
       "26                                        NaN  \n",
       "27                                        NaN  \n",
       "\n",
       "[5 rows x 23 columns]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(results_path)\n",
    "df1 = pd.read_csv(log_dir+'/training.csv')\n",
    "max_idx = df1['val_macc'].idxmax()\n",
    "new_entry = {'Filename':log_name,'Weight Initialization':'he_normal',\n",
    "\t 'Activation':activation_function+'-sigmoid','Class weights':addweights,\n",
    "\t 'Kernel Size':kernel_size,'Max Norm':maxnorm,\n",
    "\t 'Dropout -filters':dropout_rate,\n",
    "\t 'Dropout - dense':dropout_rate_dense,\n",
    "\t 'L2 - filters':l2_reg,'L2- dense':l2_reg_dense,\n",
    "\t 'Batch Size':batch_size,'Optimizer':'Adam','Learning Rate':lr,\n",
    "\t 'BN momentum':bn_momentum,\n",
    "\t 'Best Val Acc Per Cardiac Cycle':df1.loc[[max_idx]]['val_acc'].values[0]*100,\n",
    "\t 'Epoch':df1.loc[[max_idx]]['epoch'].values[0],\n",
    "\t 'Training Acc per cardiac cycle':df1.loc[[max_idx]]['acc'].values[0]*100,\n",
    "\t 'Specificity':df1.loc[[max_idx]]['val_specificity'].values[0]*100,\n",
    "\t 'Macc':df1.loc[[max_idx]]['val_macc'].values[0]*100,\n",
    "\t 'Sensitivity ':df1.loc[[max_idx]]['val_sensitivity'].values[0]*100}\n",
    "\n",
    "index,_=df.shape\n",
    "new_entry = pd.DataFrame(new_entry,index=[index])\n",
    "df2 = pd.concat([df,new_entry],axis=0)\n",
    "df2 = df2.reindex(df.columns,axis=1)\n",
    "#df2.to_csv(results_path,index=False)\n",
    "df2.tail()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "84.105960264900006"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.loc[[max_idx]]['val_sensitivity'].values[0]*100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
